package com.example.nativemedia;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import android.util.Log;

public class Buffer {
	public final static int BUFFER_SIZE = 1880000*2;
	public final static int EMPTY 	= 0;
	public final static int WRITING = 1;
	public final static int READING = 2;
	public final static int FULL    = 3;
	public final static int USED   	= 4;
	//private int bufferSize = 1880000;
	private int index,startByte,endByte,state,numConnection;
	private URL mURL;
	private ArrayList<DownloadThread> listDownloadThread;
	public Buffer(int index, int startByte, int endByte, URL mURL){
		this.index = index;
		this.startByte = startByte;
		this.endByte =  endByte;
		this.mURL = mURL;
		this.numConnection = 4;
		state = EMPTY;
	}
	public int startByte(){
		return startByte;
	}
	public int endByte(){
		return endByte;
	}
	public int getState(){
		return state;
	}
	public String getFile(){
		if((index % 2) != 0){
			return "/mnt/sdcard/playingBF1.dat";
		}else{
			return "/mnt/sdcard/playingBF2.dat";
		}
	}
	private void log(String ss){
		Log.d("nna",ss);
	}
	private class HttpDownloadThread extends DownloadThread{

		int iConnection;
		public HttpDownloadThread(int index, int threadID,  URL url, String outputFile,	int startByte, int endByte) {
			
			super(threadID, url,  outputFile, startByte, endByte);
			iConnection = index;
			// TODO Auto-generated constructor stub
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			BufferedInputStream in = null;
			RandomAccessFile raf   = null;
			try {
				// open Http connection to URL
				HttpURLConnection conn = (HttpURLConnection)mURL.openConnection();
				// set the range of byte to download
				String byteRange = mStartByte + "-" + mEndByte;
				conn.setRequestProperty("Range", "bytes=" + byteRange);
				// connect to server
				conn.connect();
				in = new BufferedInputStream(conn.getInputStream());
				// open the output file and seek to the start location
				raf = new RandomAccessFile(mOutputFile, "rw");
				log("bytes=" + byteRange+";"+(mStartByte - BUFFER_SIZE*(mThreadID - 1)));
				raf.seek(mStartByte - BUFFER_SIZE*(mThreadID - 1));
				byte data[] = new byte[4096];
				int numRead;
				while( ((numRead = in.read(data,0,4096)) != -1)){
					raf.write(data,0,numRead);
				}
				mIsFinished = true;
			} catch (IOException e) {
				log("error, try again");
				download();
			} finally {
				if (raf != null) {
					try {
						raf.close();
					} catch (IOException e) {}
				}
				
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {}
				}
			}
			
			log("End thread " + iConnection);
		}
	}
	
	public int download(){
		listDownloadThread = new ArrayList<DownloadThread>();
		HttpDownloadThread aThread;
		int i,mStart,mEnd,blockSize;
		numConnection = 4;
		blockSize = BUFFER_SIZE/numConnection;
		log("start download:"+index);
		
		for( i=0;i<numConnection;i++){
			mStart = startByte + i*blockSize;
			mEnd = mStart + blockSize-1;
			aThread = new HttpDownloadThread(i,index, mURL, getFile(), mStart,mEnd);
			listDownloadThread.add(aThread);
		}
		for(i = 0; i < numConnection; i++){
			while(!listDownloadThread.get(i).isFinished());
		}
		log("finish download:"+index);
		return 0;

	}
	
}
